local snow     = require("meet_your_mods.mhr").snow
local shortcut = require("meet_your_mods.shortcut")
local setting  = require("meet_your_mods.setting")
local ui       = require("meet_your_mods.ui")

snow.gui.GuiDangoLog:fetch_method("request_start_for_demo", "reqDangoLogStartForDemo(snow.gui.GuiDangoLog.DangoLogParam)")
snow.gui.GuiDangoLog:fetch_method("request_start",          "reqDangoLogStart(snow.gui.GuiDangoLog.DangoLogParam)")
snow.data.ContentsIdSystem.ItemId:fetch_enum("meal_ticket", "I_Normal_0124")

local dangoist = {
    setting = {
        skip_event     = true,
        auto_ticket    = true,
        super_ticket   = true,
        unlimit_ticket = false,
    },
    events = {
        [snow.eventcut.EventId:fetch_enum("cook_plaza",   "evc3503")] = true, --广场烹饪动画
        [snow.eventcut.EventId:fetch_enum("eat_plaza",    "evc3504")] = true, --广场用餐动画
        [snow.eventcut.EventId:fetch_enum("bbq_plaza",    "evc3505")] = true, --广场烧烤动画

        [snow.eventcut.EventId:fetch_enum("cook_village", "evc3026")] = true, --村烹饪动画
        [snow.eventcut.EventId:fetch_enum("eat_village",  "evc3025")] = true, --村用餐动画
        [snow.eventcut.EventId:fetch_enum("bbq_village",  "evc3033")] = true, --村烧烤动画

        [snow.eventcut.EventId:fetch_enum("cook_hub",     "evc3027")] = true, --集会所烹饪动画
        [snow.eventcut.EventId:fetch_enum("eat_hub",      "evc3024")] = true, --集会所用餐动画
        [snow.eventcut.EventId:fetch_enum("bbq_hub",      "evc3034")] = true, --集会所烧烤动画
    },
    current_event = nil,
    log_parameter = nil,
    log_ui        = nil,

    log_time    = 300,
    ticket_rate = 40,
}

function dangoist:set_action_slider(show)
    snow.gui.GuiManager:get_singleton().guiHudItemActionSliderUIObject:get_instance():write_byte(0x13, show and 1 or 0)
end

function dangoist.pre_playEvent(self, parameters)
    if not self.setting.skip_event then
        return sdk.PreHookResult.CALL_ORIGINAL
    end

    self.current_event = snow.eventcut.UniqueEventManager(sdk.to_managed_object(parameters[2]))
    if not self.events[self.current_event:get_Id()] then
        self.current_event = nil
    end

    return sdk.PreHookResult.CALL_ORIGINAL
end

function dangoist.post_playEvent(self, result)
    if self.setting.skip_event and self.current_event then
        self.current_event:reqFinishEvent(0)
        self.current_event = nil
    end

    return result
end

function dangoist.pre_reqDangoLogStartForDemo(self, parameters)
    if self.setting.skip_event then
        self.log_parameter = snow.gui.GuiDangoLog.DangoLogParam(sdk.to_managed_object(parameters[3]))
        return sdk.PreHookResult.SKIP_ORIGINAL
    end
    return sdk.PreHookResult.CALL_ORIGINAL
end

function dangoist.pre_doClose(self, parameters)
    if not self.setting.skip_event or not self.log_parameter then
        return sdk.PreHookResult.CALL_ORIGINAL
    end

    local ui = sdk.to_managed_object(parameters[2])
    if snow.gui.GuiDangoLog._typedef_ ~= ui:get_type_definition() then
        return sdk.PreHookResult.CALL_ORIGINAL
    end

    self:set_action_slider(true)
    self.log_parameter = nil

    return sdk.PreHookResult.CALL_ORIGINAL
end

function dangoist.post_onDestroy(self, result)
    if not self.setting.skip_event or not self.log_parameter then
        return result
    end

    self.log_ui = snow.gui.GuiManager:get_singleton():get_refGuiDangoLog()
    self.log_ui._DrawTimer = 0
    self.log_ui._DangoLogDrawTimeLimit     = self.log_time
    self.log_ui._IsDangoLogDrawTimerUpdate = true
    self:set_action_slider(false)
    self.log_ui:request_start(self.log_parameter:get_instance())

    return result
end

--关闭团子技能通知
function dangoist:pre_questInitBeforeMapActivation(_)
    if self.setting.skip_event and self.log_parameter then
        self.log_ui = snow.gui.GuiManager:get_singleton():get_refGuiDangoLog()
        self.log_ui._DrawTimer = self.log_ui._DangoLogDrawTimeLimit
    end

    return sdk.PreHookResult.CALL_ORIGINAL
end

function dangoist.pre_setMealTicketFlag(self, parameters)
    snow.facility.kitchen.MealFunc(sdk.to_managed_object(parameters[2]))._AddRateVal =
        self.setting.super_ticket and 100 or self.ticket_rate

    return sdk.PreHookResult.CALL_ORIGINAL
end

function dangoist.pre_updateList(self, parameters)
    if not self.setting.auto_ticket then
        return sdk.PreHookResult.CALL_ORIGINAL
    end

    local meal_function = snow.facility.kitchen.MealFunc(sdk.to_managed_object(parameters[2]))
    if self.setting.unlimit_ticket and 0 == meal_function:getMealTicketNum() then
        shortcut:add_item_to_box(snow.data.ContentsIdSystem.ItemId.meal_ticket, 1)
    end
    meal_function:setMealTicketFlag(true)

    return sdk.PreHookResult.CALL_ORIGINAL
end

function dangoist.pre_order(self, parameters)
    if self.setting.unlimit_ticket
        and snow.facility.kitchen.MealFunc(sdk.to_managed_object(parameters[2])):getMealTicketFlag() then
        shortcut:add_item_to_box(snow.data.ContentsIdSystem.ItemId.meal_ticket, 1)
    end
    return sdk.PreHookResult.CALL_ORIGINAL
end

function dangoist.initialize(self)
    snow.eventcut.UniqueEventManager.playEvent:sdk_auto_hook(self)
    snow.eventcut.UniqueEventManager.onDestroy:sdk_auto_hook(self)
    snow.gui.GuiDangoLog.request_start_for_demo:sdk_auto_hook(self)
    snow.gui.GuiDangoLog.doClose:sdk_auto_hook(self)
    snow.stage.StageManager.questInitBeforeMapActivation:sdk_auto_hook(self)
    snow.facility.kitchen.MealFunc.setMealTicketFlag:sdk_auto_hook(self)
    snow.facility.kitchen.MealFunc.updateList:sdk_auto_hook(self)
    snow.facility.kitchen.MealFunc.order:sdk_auto_hook(self)

    setting:bind_setting("dangoist", self.setting)

    ui:cluster("dango", "团子便利功能")
    ui:checkbox("dango", "dangoist", "skip_event",     "跳过所有动画")
    ui:checkbox("dango", "dangoist", "auto_ticket",    "自动使用饭票")
    ui:checkbox("dango", "dangoist", "super_ticket",   "饭票确定触发")
    ui:checkbox("dango", "dangoist", "unlimit_ticket", "无限饭票")
end

function dangoist:normalize()
end

return dangoist